package ml.kupin.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import ml.kupin.common.constant.KuPinConstant;

import java.io.Serializable;
import java.util.List;

/**
 * Created by 小柯(Allen Kerr) on 17.4.26 9:13.
 */
public class KuPinResult implements Serializable {
    // 响应业务状态，200为成功，500为失败
    private Integer status;
    // 响应消息
    private String msg;
    // 响应中的数据
    private Object data;

    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    public static KuPinResult build(String msg) {
        return new KuPinResult(null, msg, null);
    }

    public static KuPinResult build(String msg, Object data) {
        return new KuPinResult(null, msg, data);
    }

    public static KuPinResult build(Integer status, Object data) {
        return new KuPinResult(status, null, data);
    }

    public static KuPinResult build(Integer status, String msg, Object data) {
        return new KuPinResult(status, msg, data);
    }

    public static KuPinResult build(Integer status, String msg) {
        return new KuPinResult(status, msg, null);
    }

    public static KuPinResult ok(Object data) {
        return new KuPinResult(data);
    }

    public static KuPinResult ok() {
        return new KuPinResult(null);
    }

    public static KuPinResult notOk(Object data) {
        return new KuPinResult(KuPinConstant.KuPinResult_status_fail, "Not OK", data);
    }

    public static KuPinResult notOk(String msg, Object data) {
        return new KuPinResult(KuPinConstant.KuPinResult_status_fail, msg, data);
    }

    public static KuPinResult notOk() {
        return new KuPinResult(KuPinConstant.KuPinResult_status_fail, "Not OK", null);
    }

    public KuPinResult() {
    }

    public KuPinResult(Integer status, String msg, Object data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public KuPinResult(Object data) {
        this.status = KuPinConstant.KuPinResult_status_success;
        this.msg = "OK";
        this.data = data;
    }

    @Override
    public String toString() {
        return "KuPinResult{" +
                "status=" + status +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    /*public Boolean isOK() {
        return this.status == KuPinConstant.KuPinResult_status_success;
    }*/

    /**
     * 将json结果集转化为KupinResult对象，使用Jackson
     *
     * @param jsonData json数据
     * @param clazz    KupinResult中的object类型
     * @return
     */
    public static KuPinResult formatToPojoWithJackson(String jsonData, Class<?> clazz) {
        try {
            if (clazz == null) {
                return MAPPER.readValue(jsonData, KuPinResult.class);
            }
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (clazz != null) {
                if (data.isObject()) {
                    obj = MAPPER.readValue(data.traverse(), clazz);
                } else if (data.isTextual()) {
                    obj = MAPPER.readValue(data.asText(), clazz);
                }
            }
            return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将json结果集转化为KupinResult对象，使用FastJson
     *
     * @return
     */
    public static KuPinResult formatToPojoWithFastJson(String jsonData, Class<?> clazz) {
        try {
            if (clazz == null) {
                return JSON.parseObject(jsonData, KuPinResult.class);
            }
            JSONObject jsonObject = JSON.parseObject(jsonData);
            Object data = jsonObject.getObject("data", clazz);
            return build(jsonObject.getIntValue("status"), jsonObject.getString("msg"), data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 没有object对象的转化，使用Jackson
     *
     * @param json json数据
     * @return
     */
    public static KuPinResult formatWithJackson(String json) {
        try {
            return MAPPER.readValue(json, KuPinResult.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 没有object对象的转化，使用FastJson
     *
     * @param json json数据
     * @return
     */
    public static KuPinResult formatWithFastJson(String json) {
        try {
            return JSON.parseObject(json, KuPinResult.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Object是集合转化，使用Jackson
     *
     * @param jsonData json数据
     * @param clazz    集合中的类型
     * @return
     */
    public static KuPinResult formatToListWithJackson(String jsonData, Class<?> clazz) {
        try {
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (data.isArray() && data.size() > 0) {
                obj = MAPPER.readValue(data.traverse(),
                        MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
            }
            return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Object是集合转化，使用FastJson
     *
     * @param jsonData json数据
     * @param clazz    集合中的类型
     * @return
     */
    public static KuPinResult formatToListWithFastJson(String jsonData, Class<?> clazz) {
        try {
            JSONObject jsonObject = JSON.parseObject(jsonData);
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            List<?> list = null;
            if (jsonArray != null && jsonArray.size() > 0) {
                list = jsonArray.toJavaList(clazz);
            }
            return build(jsonObject.getIntValue("status"), jsonObject.getString("msg"), list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
